//
//  xianXiaBaseViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/9/14.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "xianXiaBaseViewController.h"
#import "UIParameter.h"
#import "Macros.h"
#import "baseTableViewCell.h"
#import "allActivity.h"
#import "UIImageView+WebCache.h"
#import "xianXiaDetailViewController.h"

@interface xianXiaBaseViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSString *indexTag;
}
@property (nonatomic, strong) UITableView *myTableView;
@property(nonatomic,copy)NSMutableArray *tableViewArray;
@property(nonatomic,retain) baseTableViewCell *myCell;
@property(nonatomic,copy)NSMutableArray *idArray;
@end

@implementation xianXiaBaseViewController
@synthesize tableViewArray,myCell,idArray;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (void)createTableViewFromVC:(NSString *)yourTag
{
    idArray=[NSMutableArray array];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    indexTag = yourTag;
}
//将子类的数组负值给父类
-(void)changeArray:(NSMutableArray*)array
{
    tableViewArray=array;
//    NSLog(@"array=%@",array);
}
#pragma mark - myTableView
- (UITableView *)myTableView
{
    if (!_myTableView)
    {
        //-0.232*screenWidth
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, FUll_VIEW_WIDTH, FUll_CONTENT_HEIGHT - PageBtn) style:UITableViewStylePlain];
    }
    
    return _myTableView;
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSLog(@"tableViewArray=%@",tableViewArray);
    return tableViewArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    allActivity *aa=[allActivity shardeManager];
    myCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (myCell == nil)
    {
        myCell = [[[NSBundle mainBundle] loadNibNamed:@"baseTableViewCell" owner:self options:nil]lastObject];
        myCell.selectionStyle=UITableViewCellSelectionStyleNone;

    }
    if (tableViewArray.count>0)
    {
        aa.contentString=[tableViewArray[indexPath.row] objectForKey:@"content"];
        aa.picString=[tableViewArray[indexPath.row] objectForKey:@"pic"];
        aa.sellerString=[tableViewArray[indexPath.row] objectForKey:@"seller"];
        aa.tpriceString=[tableViewArray[indexPath.row] objectForKey:@"tprice"];
        aa.idString=[tableViewArray[indexPath.row] objectForKey:@"id"];
        aa.titleString=[tableViewArray[indexPath.row] objectForKey:@"type"];
//        NSLog(@"aa.titleString=%@",aa.titleString);
        [idArray addObject:aa.idString];
        myCell.nameLabel.text=aa.sellerString;
        myCell.tuanGou.textColor=pinkColor;
        myCell.contentLabel.text=aa.contentString;
        myCell.priceLabel.textColor=pinkColor;
        myCell.priceLabel.text=[NSString stringWithFormat:@"¥%@",aa.tpriceString];
    }
    NSURL* imagePath = [NSURL URLWithString:aa.picString];
    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
    [myCell.picImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
    return myCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    myCell=[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return myCell.frame.size.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *idString=idArray[indexPath.row];
    allActivity *aa=[allActivity shardeManager];
    aa.idString=idString;
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    NSString *num=[NSString stringWithFormat:@"%d",aa.num];
    [userDefaults setObject:num forKey:@"aa.num"];
    NSLog(@"num=%@",num);
    xianXiaDetailViewController *xxdv=[xianXiaDetailViewController new];
    [self.navigationController pushViewController:xxdv animated:YES];
}

- (void)testAction
{
    if (isDebug)
    {
        NSLog(@"测试点击事件");
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
